package java_thinking.unit_9._9_6;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;

/**
 * @Description :
 * @author: cww
 * @DateTime: 2019-03-05 21:52
 */

public class RandWords implements Readable {
    /**
     * new Random(7) 伪随机数生成器的内部状态的初始值
     */
    private static Random random = new Random(7);
    private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final char[] LOWERS = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    private static final char[] VOWELS = "aeiou".toCharArray();
    private int count;
    private RandWords(int count){
        this.count = count;
    }
    /**
     */
    @Override
    public int read(CharBuffer cb) throws IOException {
        if (count-- == 0) {return -1;}
        cb.append(CHARS[random.nextInt(CHARS.length)]);
        for (int i = 0;i<4;i++){
            cb.append(VOWELS[random.nextInt(VOWELS.length)]);
            cb.append(LOWERS[random.nextInt(LOWERS.length)]);
        }
        cb.append(" ");
        return 10;

    }

//    public static void maindemo(String[] args) {
//        Scanner s = new Scanner(new RandWords(10));
//        while (s.hasNext()){
//            System.out.println(s.next());
//        }
//    }
}
class RandomDoubles {
    private static Random random = new Random(47);
    public double next(){
        return random.nextDouble();
    }
}

/**
 * 适配器模式
 */
class AdapterRandomDoubles extends RandomDoubles implements Readable{
    private int count;
    public AdapterRandomDoubles(int count){
        this.count = count;
    }
    @Override
    public int read(CharBuffer cb){
        if (count == 0) {
            return -1;
        }
        String result = Double.toString(next())+" ";
        cb.append(result);
        return result.length();
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(new AdapterRandomDoubles(7));
        while (s.hasNextDouble()){
            System.out.println(s.nextDouble());
        }
    }
}